#! /usr/bin/sdlbrt

finp$="test2z.csv"
dim cellc[40,25],cella[40,25]

function locstrinfo$(a$,b$,c$)
  '-bug to be fixed, find where
  adra=1:adrb=1:blngt=len(b$):d$=""
  while adrb<=blngt
  if mid$ (a$,adra,1)=mid$ (b$,adrb,1) then:adrb+=1:end if
  adra+=1:end while
  while mid$(a$,adra,1)<>left$(c$,1):d$=d$+mid$(a$,adra,1):adra+=1:end while
  locstrinfo$=d$
  end function

function aquariuspalette(hxc$)
  hxv=val("0x"+hxc$)
  b=(hxv mod 256):hxv=int(hxv/256)
  g=(hxv mod 256):hxv=int(hxv/256)
  r=(hxv mod 256):hxv=int(hxv/256)
  tqv=0
  if g>128 then:tqv=tqv+2:end if
  if r>128 then:tqv=tqv+1:end if
  if b>128 then:tqv=tqv+4:end if
  if tqv=7 and g<224 then:tqv=8:end if
  if tqv=6 and g<180 then:tqv=9:end if
  if tqv=5 and r<216 then:tqv=10:end if
  if tqv=4 and b<176 then:tqv=11:end if
  if tqv=3 and b>64 then:tqv=12:end if
  if tqv=2 and g<220 then:tqv=13:end if
  if tqv=1 and r<204 then:tqv=14:end if
  if tqv=0 and g>16 then:tqv=15:end if
  aquariuspalette=tqv
  end function

open finp$ for input as #1
open finp$+".bin" for output as #2

while eof(1)=0
  file input #1,tlq$
  if left$(tlq$,4)<>"X,Y," then:
    tlq$=">"+tlq$+",0,0,0,0,0,0,0,0,"
    xv$=locstrinfo$(tlq$,">",",")
    yv$=locstrinfo$(tlq$,">,",",")
    ch$=locstrinfo$(tlq$,">,,",",")
    ik$=locstrinfo$(tlq$,">,,,#",",")
    pp$=locstrinfo$(tlq$,">,,,,#",",")
    cellc[val(xv$),val(yv$)]=val(ch$)
    cella[val(xv$),val(yv$)]=aquariuspalette(ik$)*16+aquariuspalette(pp$)
    end if
  wend

for ypos=0 to 23
  for xpos=0 to 39
    writebyte(2,cellc[xpos,ypos])
    next:next
for ypos=0 to 63
    writebyte(2,0)
    next
for ypos=0 to 23
  for xpos=0 to 39
    writebyte(2,cella[xpos,ypos])
    next:next

close #1:close #2


